<?php

namespace App\Http\Service;

use App\Libs\HttpCurlLibrary;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use App\Models\Postai;

//AI 存储时间
class PostaiService extends BaseService
{
   
    public function __construct(Postai $model)
    {
        parent::__construct();
        $this->model=$model;
    }

    #全部内容
    public function  getList($request,$data=[])
    {
        $where=[
            'unique_id'=>$data['unique_id'],
        ];
        $is_expired=$data['is_expired'];
        $fileds=['unit_price','commission_rate','commission_amount','unique_id','user_id','avatar','nickname','create_time','publish_time','title'];
        $result=[];
        ///dd($is_expired);
        if($is_expired!='1'){# 不过期不用最后一条ai
            $list=$this->model->where($where)->select($fileds)->get()->toArray();
            foreach($list as $k=>$v){
                $v['type']='baoliao';
                $v['class']=1;
                $publish_time=$v['publish_time'];
                $day_3=date('Y-m-d H:i:s', strtotime('-3 days'));
                #不过期用历史记录
                if($publish_time<$day_3){
                    $result[]=$v;
                }
            }
        }else{
            # =1 过期用ai
            $result=$this->model->where($where)->select($fileds)->get()->toArray();
            foreach($result as $k=>&$v){
                $v['type']='baoliao';
                $v['class']=1;
            }
        }

        
        return $result;


    }
    public  function addData($request,$data=[])
    {
        $data['create_time']=date("Y-m-d H:i:s");
        $data['update_time']=date("Y-m-d H:i:s");
        $data['publish_time']=date("Y-m-d H:i:s");
        $where=[
            'unique_id'=>$data['unique_id'],
        ];
        
        $json_data=json_decode($data['json_data'],true);
        if(!empty($json_data)){
            if(isset($json_data['name'])){
                $data['title']=$json_data['name'];
            }
            if(isset($json_data['final_price'])){
                $data['unit_price']=$json_data['final_price'];
            }
            if(isset($json_data['commission_rate'])){
                $data['commission_rate']=$json_data['commission_rate'];
            }
            if(isset($json_data['commission'])){
                $data['commission_amount']=$json_data['commission'];
            }
            
            
            $data['user_id']=0;
            $data['nickname']='乐天未来';
            $data['avatar']='http://xingfanapp.oss-cn-hangzhou.aliyuncs.com/xingfanapp/uploads/442ca8601a70923b7fdb5d697188f80c3983df5c.png';
        }
        //dd($json_data);
        $first=$this->model
            ->orderBy('create_time', 'desc')
            ->where($where)->first();
        //dd($first);
        $result=[];
        if($first){
            $publish_time=$first['publish_time'];
            //dd($publish_time);
            #判断发布时间
            $day_3=date('Y-m-d H:i:s', strtotime('-3 days'));
            if($publish_time<$day_3){
                #发布时间不在近3天,添加
                $insertId=$this->model->insertGetId($data);
                $ai_info=$this->model->where($where)->first();
                $publish_time=$ai_info['publish_time'];
            }

        }else{
            
            $insertId=$this->model->insertGetId($data);
            $ai_info=$this->model->where($where)->first();
            $publish_time=$ai_info['publish_time'];
        }
        
        $result=[
            'publish_time'=>$publish_time
        ];
       // dd($result);
        return $result;
    }
    

}
